package com.os;

import com.graphics.ImageIdentity;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class ObjectSpriteControlPanel implements TreeSelectionListener, ActionListener, ChangeListener {

    private Main comp;
    private String currentPosition = "";
    private transient volatile ObjectSprite currentSprite;
    private SpriteAnimator spriteAnimator;

    public ObjectSpriteControlPanel(Main comp) {
        this.comp = comp;
        comp.ObjectSpriteButtonAddToPattern.addActionListener(this);
        comp.ObjectSpriteButtonSetOnWorkspace.addActionListener(this);
        comp.ObjectSpriteButtonRename.addActionListener(this);
        comp.ObjectSpriteButtonRemove.addActionListener(this);
        comp.ObjectSpriteButtonPlay.addActionListener(this);
        comp.ObjectSpriteButtonPause.addActionListener(this);
        comp.ObjectSpriteButtonStop.addActionListener(this);
        comp.ObjectSpriteDelayAnimationTime.addChangeListener(this);
        comp.AddNew.addActionListener(this);
        comp.Remove.addActionListener(this);
        comp.Edit.addActionListener(this);
        spriteAnimator = new SpriteAnimator();
    }

    public void showImgeManagerInfo(boolean activate) {
        comp.ObjectSpriteButtonAddToPattern.setVisible(activate);
        comp.ObjectSpriteButtonRename.setVisible(activate);
        comp.ObjectSpriteButtonRemove.setVisible(activate);
        comp.ObjectSpriteButtonSetOnWorkspace.setVisible(activate);
        comp.ObjectSpriteButtonPlay.setVisible(activate);
        comp.ObjectSpriteButtonPause.setVisible(activate);
        comp.ObjectSpriteButtonStop.setVisible(activate);
    }

    public void valueChanged(TreeSelectionEvent e) {
        if(!comp.project.isCreated())return;
        try {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) comp.osPanelWidget.getTree().getLastSelectedPathComponent();
            comp.ObjectSpriteRightPanelRender.setVisible(false);
            if (node == null) {
                return;
            } else if (node.isLeaf()) {
                System.out.println("leaf");
                currentSprite = (ObjectSprite) ((DefaultMutableTreeNode) node.getParent()).getUserObject();
                currentPosition = node.toString();
                setPositionBoundingBox();
                show();
            } else if (!node.isRoot()) {
                currentSprite = (ObjectSprite) node.getUserObject();
                currentPosition = "";
                showImgeManagerInfo(true);
                comp.ObjectSpriteButtonAddToPattern.setVisible(false);
                comp.ObjectSpriteButtonPlay.setVisible(false);
                comp.ObjectSpriteButtonPause.setVisible(false);
                comp.ObjectSpriteButtonStop.setVisible(false);
                System.out.println("parent");
            } else {
                System.out.println("salah");
                currentPosition = "";
                currentSprite = null;
                showImgeManagerInfo(false);
            }
        } catch (Exception err) {
        }
    }

    public void actionPerformed(ActionEvent e) {
        if(!comp.project.isCreated())return;
        Object ob = e.getSource();
        if (ob == comp.ObjectSpriteButtonAddToPattern) {
            OSDialogAddFrame daf = new OSDialogAddFrame(comp);
            spriteAnimator.setVisible(true);
            daf.setModal(true);
            daf.setVisible(true);
        } else if (ob == comp.ObjectSpriteButtonPlay) {
            System.out.println("oceee,,,,,");
            spriteAnimator.setVisible(true);
            spriteAnimator.playAnim();
            comp.ObjectSpriteButtonPlay.setEnabled(false);
            comp.ObjectSpriteButtonPause.setEnabled(true);
            comp.ObjectSpriteButtonStop.setEnabled(true);
        } else if (ob == comp.ObjectSpriteButtonPause) {
            spriteAnimator.pauseAnim();
            comp.ObjectSpriteButtonPlay.setEnabled(true);
            comp.ObjectSpriteButtonPause.setEnabled(false);
            comp.ObjectSpriteButtonStop.setEnabled(true);
        } else if (ob == comp.ObjectSpriteButtonStop) {
            spriteAnimator.stopAnim();
            comp.ObjectSpriteButtonPlay.setEnabled(true);
            comp.ObjectSpriteButtonPause.setEnabled(false);
            comp.ObjectSpriteButtonStop.setEnabled(false);
        } else if (ob == comp.ObjectSpriteButtonRename) {
            comp.osContainer.renameSprites(currentSprite, "Geul");
            comp.osPanelWidget.reloadAll();
        } else if (ob == comp.ObjectSpriteButtonRemove) {
            spriteAnimator.setVisible(false);
            comp.osContainer.removeSprites(currentSprite);
            comp.osPanelWidget.reloadAll();
        } else if (ob == comp.AddNew) {
            OSDialogBoundingBox bounding = new OSDialogBoundingBox(comp);
            bounding.setModal(true);
            bounding.setVisible(true);
        } else if (ob == comp.Edit) {
            try {
                OSBoundingBox osb = (OSBoundingBox) currentBound.get(comp.ObjectSpriteListOfBound.getSelectedIndex());
                OSDialogBoundingBox bounding = new OSDialogBoundingBox(comp);
                bounding.setBound(osb);
                bounding.setModal(true);
                bounding.setVisible(true);
            } catch (Exception err) {
            }
        } else if (ob == comp.Remove) {
            int list[] = comp.ObjectSpriteListOfBound.getSelectedIndices();
            for (int i = 0; i < list.length; i++) {
                removeBoundingBox(list[i]);
            }
        }
    }
    ArrayList currentBound = null;

    public void setPositionBoundingBox() {
        if (currentPosition == ObjectSprite.OTHER_SPRITE) {
            currentBound = currentSprite.getOtherBound();
        } else if (currentPosition == ObjectSprite.LEFT_SPRITE) {
            currentBound = currentSprite.getLeftBound();
        } else if (currentPosition == ObjectSprite.RIGHT_SPRITE) {
            currentBound = currentSprite.getRightBound();
        } else if (currentPosition == ObjectSprite.UP_SPRITE) {
            currentBound = currentSprite.getUpBound();
        } else if (currentPosition == ObjectSprite.DOWN_SPRITE) {
            currentBound = currentSprite.getDownBound();
        }
    }

    public void addBoundingBox(OSBoundingBox bound) {
        currentBound.add(bound);
        showBoundingBox();
    }

    public void editBoundingBox(OSBoundingBox src, OSBoundingBox dst) {
        currentBound.set(currentBound.indexOf(src), dst);
        showBoundingBox();
    }

    public void removeBoundingBox(int index) {
        currentBound.remove(index);
        showBoundingBox();
    }

    public void showBoundingBox() {
        comp.ObjectSpriteListOfBound.setListData(currentBound.toArray());
    }

    public void setFrame(ImageIdentity ident) {
        if (currentPosition == ObjectSprite.OTHER_SPRITE) {
            currentSprite.setOther(ident);
        } else if (currentPosition == ObjectSprite.LEFT_SPRITE) {
            currentSprite.setLeft(ident);
        } else if (currentPosition == ObjectSprite.RIGHT_SPRITE) {
            currentSprite.setRight(ident);
        } else if (currentPosition == ObjectSprite.UP_SPRITE) {
            currentSprite.setUp(ident);
        } else if (currentPosition == ObjectSprite.DOWN_SPRITE) {
            currentSprite.setDown(ident);
        }
    }

    public void setSpriteAnimation(boolean started) {
        if (currentSprite != null) {
            if (spriteAnimator.isRunning()) {
                spriteAnimator.stop();
            }
            try {
                BufferedImage bi = null;
                int width, height, time = 0;
                ArrayList tmp = null;
                if (currentPosition == ObjectSprite.OTHER_SPRITE) {
                    tmp = currentSprite.getOther().getImages();
                    time = currentSprite.getOtherTime();
                } else if (currentPosition == ObjectSprite.LEFT_SPRITE) {
                    tmp = currentSprite.getLeft().getImages();
                    time = currentSprite.getLeftTime();
                } else if (currentPosition == ObjectSprite.RIGHT_SPRITE) {
                    tmp = currentSprite.getRight().getImages();
                    time = currentSprite.getRightTime();
                } else if (currentPosition == ObjectSprite.UP_SPRITE) {
                    tmp = currentSprite.getUp().getImages();
                    time = currentSprite.getUpTime();
                } else if (currentPosition == ObjectSprite.DOWN_SPRITE) {
                    tmp = currentSprite.getDown().getImages();
                    time = currentSprite.getDownTime();
                }
                bi = (BufferedImage) tmp.get(0);
                spriteAnimator.setSprite(tmp, time, started);
                width = bi.getWidth() + 20;
                height = bi.getHeight() + 20;
                spriteAnimator.setSize(width, height);
                System.out.println("wh" + width + " h=" + height);
                comp.ObjectSpriteRightPanelRender.add(spriteAnimator).setBounds(200, 10, width, height);
                comp.ObjectSpriteRightPanelRender.setPreferredSize(new Dimension(width + 200, height + 200));
                spriteAnimator.start();
                comp.ObjectSpriteRightPanelRender.setVisible(true);
            } catch (Exception e) {
                comp.ObjectSpriteRightPanelRender.setVisible(false);
                comp.ObjectSpriteButtonPlay.setVisible(false);
                comp.ObjectSpriteButtonPause.setVisible(false);
                comp.ObjectSpriteButtonStop.setVisible(false);
            }
            comp.ObjectSpriteRightPanelRender.repaint();
            comp.ObjectSpriteRightPanelRender.revalidate();
        }
    }

    public void stateChanged(ChangeEvent e) {
        try {
            int time = Integer.parseInt("" + comp.ObjectSpriteDelayAnimationTime.getValue());
            if (time < 0) {
                time = 0;
                comp.ObjectSpriteDelayAnimationTime.setValue(time);
            }
            if (currentPosition == ObjectSprite.OTHER_SPRITE) {
                currentSprite.setOtherTime(time);
            } else if (currentPosition == ObjectSprite.LEFT_SPRITE) {
                currentSprite.setLeftTime(time);
            } else if (currentPosition == ObjectSprite.RIGHT_SPRITE) {
                currentSprite.setRightTime(time);
            } else if (currentPosition == ObjectSprite.UP_SPRITE) {
                currentSprite.setUpTime(time);
            } else if (currentPosition == ObjectSprite.DOWN_SPRITE) {
                currentSprite.setDownTime(time);
            }
            System.out.println("Time Animtion = " + time);
            show();
        } catch (Exception err) {
        }
    }

    public void show() {
        showImgeManagerInfo(true);
        setSpriteAnimation(true);
        spriteAnimator.stopAnim();
        showBoundingBox();
        comp.ObjectSpriteButtonPlay.setEnabled(true);
        comp.ObjectSpriteButtonPause.setEnabled(false);
        comp.ObjectSpriteButtonStop.setEnabled(false);
    }
}
